import csv
import glob
import os
import xlwt

if __name__ == "__main__":
    csv_folder = "target"  # CSV文件所在文件夹
    excel_folder = "target"  # Excel文件保存文件夹

    # 获取所有以 .csv 结尾的文件路径
    csv_files = glob.glob(os.path.join(csv_folder, "*.csv"))

    for csv_file in csv_files:
        # 获取CSV文件名（不包含路径和后缀）
        csv_filename = os.path.splitext(os.path.basename(csv_file))[0]

        # 创建Excel文件
        excel_file = xlwt.Workbook(encoding="utf-8")

        # 创建sheet工作表
        sheet = excel_file.add_sheet("sheet1", cell_overwrite_ok=True)

        # 读取CSV文件并写入Excel文件
        with open(csv_file, newline="", encoding="utf-8") as f:
          rows = csv.reader(f)
          for iRow, row in enumerate(rows):
             for iCol in range(len(row)):
              if iCol == 0:
                # Skip the first column if it contains non-data information
                continue
              if iCol in (4,5,7):
                # Format the fourth column as a number with two decimal places
                style = xlwt.XFStyle()
                style.num_format_str = '0.00'
                sheet.write(iRow, iCol-1, float(row[iCol]), style)
              else:
                sheet.write(iRow, iCol-1, row[iCol])

        # 保存Excel文件
        excel_filename = os.path.join(excel_folder, f"{csv_filename}.xls")
        excel_file.save(excel_filename)